// packageE/groupCode/groupCodeCreate/groupCodeCreate.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    explain: false,
    swlist: [],
    upper_limit: [],
    info: [],
    swlistImg: [],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getData();
    let arr = [
      { code_url: "二维码1", upper_limit: 300 },
      { code_url: "二维码2", upper_limit: 300 },
    ];
    console.log(arr);
  },
  name(e) {
    let val = e.detail.value;
    this.setData({
      code_name: val,
    });
  },
  guide(e) {
    let val = e.detail.value;
    this.setData({
      code_guide: val,
    });
  },
  service_guide(e) {
    let val = e.detail.value;
    this.setData({
      customer_service_guide: val,
    });
  },
  change(e) {
    console.log(e);
    let val = e.detail.value;
    this.setData({
      explain: val,
    });
  },
  change1(e) {
    let val = e.detail.value;
    this.setData({
      recommend: val,
    });
  },
  // 选择图片
  onRead() {
    let that = this;
    let urlStr = app.getNetAddresss("upload.uploadPic");
    wx.chooseImage({
      count: 1,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        const tempFilePaths = res.tempFilePaths;

        wx.compressImage({
          src: tempFilePaths[0], // 图片路径
          quality: 60, // 压缩质量
          success(e) {
            wx.showLoading({
              title: "上传中",
            });
            let photourl = e.tempFilePath;
            wx.uploadFile({
              url: urlStr,
              filePath: photourl,
              name: "file",
              success(resdata) {
                wx.hideLoading({
                  success: (res) => {},
                });
                var res = JSON.parse(resdata.data);
                if (res.result == 1) {
                  console.log(photourl, res.data.img);
                  that.setData({
                    imageUrl: res.data.img,
                    customer_service: res.data.img_url,
                  });
                } else {
                  wx.showToast({
                    icon: "none",
                    title: res.msg,
                    duration: 1500,
                  });
                }
              },
            });
          },
          fail(e) {},
          complete(e) {},
        });
      },
    });
  },
  // 群二维码  多图上传
  onRead_2() {
    let that = this;
    // let urlStr = app.getNetAddresss("upload.uploadPic");
    let ig = "1";
    wx.chooseImage({
      count: 9,
      sizeType: ["original", "compressed"],
      sourceType: ["album", "camera"],
      success(res) {
        // var tempFilePaths = res.tempFilePaths
        console.log(res.tempFilePaths);
        that.unload(
          {
            tempFilePaths: res.tempFilePaths,
          },
          ig
        );
      },
    });
  },
  //多张上传方法
  unload(data, ig) {
    console.log(ig);
    wx.showLoading({
      title: "上传中",
    });
    let urlStr = app.getNetAddresss("upload.uploadPic");
    var that = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;
    wx.uploadFile({
      url: urlStr,
      filePath: data.tempFilePaths[i],
      name: "file",
      formData: null,
      success(resdata) {
        wx.hideLoading({
          success: (res) => {},
        });
        var res = JSON.parse(resdata.data);
        console.log(res.data.img_url);
        if (ig == "1") {
          that.data.swlistImg.push(res.data.img);
          that.data.swlist.push(res.data.img_url);
          that.setData({
            code_url: that.data.swlist,
            swlistImg: that.data.swlistImg,
            // 'applyModel.information_thumb': that.data.swlist
          });
        } else if (ig == 2) {
          that.data.swlis.push(res.data.img_url);
          that.setData({
            code_url: that.data.swlis,
            swlistImg: that.data.swlistImg,
            // 'applyModel.otherImg': that.data.swlis
          });
        }

        console.log(that.data.code_url);
      },
      fail(e) {
        wx.hideLoading({
          success: (res) => {},
        });
        fail++;
      },
      complete(e) {
        wx.hideLoading({
          success: (res) => {},
        });
        i++;
        if (i == data.tempFilePaths.length) {
          //当图片传完时，停止调用
          console.log("执行完毕");
          console.log("成功：" + success + " 失败：" + fail);
        } else {
          //若图片还没有传完，则继续调用函数
          data.i = i;
          data.success = success;
          data.fail = fail;
          that.unload(data, ig); //递归，回调自己
        }
      },
    });
  },
  delImg(e) {
    let ind = e.currentTarget.dataset.ind;
    let list = this.data.code_url;
    let lis = this.data.swlistImg;
    list.splice(ind, 1);
    lis.splice(ind, 1);
    console.log(list, "截取后");
    console.log(lis, "截取后");
    this.setData({
      code_url: list,
      swlistImg: lis,
    });
  },
  // 扫码上限人数
  upperLimit(e) {
    console.log(e);
    let val = e.detail.value;
    let ind = e.currentTarget.dataset.ind;
    let upper_limit = this.data.upper_limit;
    upper_limit[ind] = val;
    this.setData({
      upper_limit: upper_limit,
    });
  },
  explaincon(e) {
    let val = e.detail.value;
    this.setData({
      explain_content: val,
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getset();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {},
  getData() {
    var that = this;
    let urlStr = app.getNetAddresss("plugin.group-code.api.group-code.add");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          that.setData({
            info: res.data.set,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  getset() {
    let urlStr = app.getNetAddresss("plugin.group-code.api.set.index");
    app._postNetWork({
      url: urlStr,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          this.setData({
            is_open: res.data.set.is_open,
            auth_recommend: res.data.set.auth_recommend,
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
  save() {
    if (app._isTextEmpty(this.data.code_name)) {
      wx.showToast({
        title: "请输入群活码名称",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (this.data.explain) {
      if (app._isTextEmpty(this.data.explain_content)) {
        wx.showToast({
          title: "请输入说明内容",
          icon: "none",
          duration: 1000,
        });
        return;
      }
    }
    if (app._isTextEmpty(this.data.customer_service)) {
      wx.showToast({
        title: "请输入客服二维码",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.customer_service_guide)) {
      wx.showToast({
        title: "请输入客服引导语",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.code_guide)) {
      wx.showToast({
        title: "请输入群活码引导语",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (app._isTextEmpty(this.data.code_url)) {
      wx.showToast({
        title: "请上传群二维码url",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    if (this.data.upper_limit.length < this.data.code_url.length) {
      console.log(this.data.upper_limit.length, this.data.code_url.length);
      console.log(this.data.upper_limit, this.data.code_url);
      console.log("请输入扫码上限人数");
      wx.showToast({
        title: "请输入扫码上限人数",
        icon: "none",
        duration: 1000,
      });
      return;
    }
    var code_data = [];
    for (let i = 0; i < this.data.code_url.length; i++) {
      code_data.push({
        code_url: this.data.swlistImg[i],
        upper_limit: this.data.upper_limit[i],
      });
    }
    let json = {
      code_data: JSON.stringify(code_data),
      code_name: this.data.code_name,
      code_guide: this.data.code_guide,
      recommend: this.data.recommend ? "1" : "0",
      explain: this.data.explain ? "1" : "0",
      explain_content: this.data.explain_content,
      customer_service: this.data.imageUrl,
      customer_service_guide: this.data.customer_service_guide,
    };
    this.saveSubmit(json);
  },
  saveSubmit(json) {
    let urlStr = app.getNetAddresss("plugin.group-code.api.group-code.create");
    app._postNetWork({
      url: urlStr,
      data: json,
      success: (resdata) => {
        var res = resdata.data;
        if (res.result == 1) {
          console.log(res);
          wx.showToast({
            title: res.msg,
            icon: "none",
            duration: 1000,
            success: function () {
              wx.navigateBack({
                delta: 1,
              });
            },
          });
        } else {
          wx.showToast({
            title: res.msg,
            icon: "none",
          });
          console.log(res.msg);
        }
      },
    });
  },
});
